package com.douqu.game.main.msg.param;

import com.douqu.game.core.controller.PlayerController;
import com.douqu.game.core.e.E_BattleDetailType;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.battle.BattleInitParam;
import com.douqu.game.core.entity.battle.BattleInitParamWorldMap;
import com.douqu.game.core.entity.world.map.SpriteBean;
import com.douqu.game.core.entity.world.map.SpritePlayerBean;
import com.douqu.game.core.entity.world.map.WorldMapData;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.WorldInfoFactory;
import com.douqu.game.core.protobuf.SGChallengeProto;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.main.GameServer;
import com.douqu.game.main.util.MsgUtils;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-01-22 20:32
 */
public class StartBattleParam
{
    private String battleId;

    private String playerIndex;

    private String targetIndex;

    private String serverHost;

    private int serverPort;

    private long createTime;

    private SGCommonProto.E_BATTLE_TYPE battleType;

    private E_BattleDetailType battleDetailType;

    private RequestParam[] params;

    private BattleInitParam initParam;

    public StartBattleParam(String battleId, SGCommonProto.E_BATTLE_TYPE battleType, E_BattleDetailType battleDetailType, String serverHost, int serverPort, String playerIndex, String targetIndex, BattleInitParam initParam, RequestParam... params)
    {
        this.battleId = battleId;
        this.battleType = battleType;
        this.battleDetailType = battleDetailType;
        this.playerIndex = playerIndex;
        this.targetIndex = targetIndex;
        this.params = params;
        this.serverHost = serverHost;
        this.serverPort = serverPort;
        this.createTime = DataFactory.currentTime;
        this.initParam = initParam;
    }


    public void sendStartBattle()
    {
        PlayerController playerController = GameServer.getInstance().getWorldManager().getPlayerController(playerIndex);
        if(playerController == null)
        {
            LogUtils.error("玩家不在线 -> " + playerIndex);
            return;
        }

        SGChallengeProto.S2C_RequestBattle.Builder response = SGChallengeProto.S2C_RequestBattle.newBuilder();
        response.setMapId(initParam.getMapId());

        if(params != null)
        {
            for(RequestParam param : params)
            {
                if(param.getValue() instanceof Player)
                {
                    Player player = (Player) param.getValue();
                    SGCommonProto.PlayerBattleBaseInfo.Builder baseInfo = SGCommonProto.PlayerBattleBaseInfo.newBuilder();
                    baseInfo.setPlayerIndex(player.getObjectIndex());
                    baseInfo.setNickName(player.getName());
                    baseInfo.setAvatar(player.avatar);
                    baseInfo.setLv(player.getLv());
                    baseInfo.setFc(player.fc);
                    baseInfo.setMasterId(player.master);

                    response.setTargetPlayer(baseInfo);
                }
                else
                {
                    response.addBattleParam(SGCommonProto.BattleParam.newBuilder().setType(param.getKey()).setValue(Integer.parseInt(param.getValue().toString())));
                }
            }
        }

        response.setBattleId(battleId);
        response.setBattleType(battleType);

        SGCommonProto.ServerInfo.Builder serverInfoData = SGCommonProto.ServerInfo.newBuilder();
        serverInfoData.setHost(serverHost);
        serverInfoData.setPort(serverPort);
        response.setServerInfo(serverInfoData);

        LogUtils.info("请求战斗参数 -》" + response);

        byte[] data = response.build().toByteArray();

        MsgUtils.sendRequestBattle(playerController, data);

        if(battleDetailType == E_BattleDetailType.PVP_WORLD_MAP)
        {
            PlayerController targetController = GameServer.getInstance().getWorldManager().getPlayerController(targetIndex);
            if(targetController != null)
            {
                MsgUtils.sendRequestBattle(targetController, data);
            }
        }

        if(initParam instanceof BattleInitParamWorldMap)
        {
            BattleInitParamWorldMap battleInitParamWorldMap = (BattleInitParamWorldMap) initParam;
            WorldMapData worldMapData = WorldInfoFactory.getInstance().getWorldMapData(battleInitParamWorldMap.getMasterPlayerIndex());
            SpriteBean spriteBean = worldMapData.getSpriteBean(playerIndex);
            if(spriteBean instanceof SpritePlayerBean)
            {
                spriteBean.setInCity(false);
                worldMapData.removeLookCity(spriteBean.getObjectIndex());
            }

            spriteBean = worldMapData.getSpriteBean(targetIndex);
            if(spriteBean instanceof SpritePlayerBean)
            {
                spriteBean.setInCity(false);
                worldMapData.removeLookCity(spriteBean.getObjectIndex());
            }
        }
    }



    public boolean isOverdue()
    {
        return DataFactory.currentTime - createTime >= 60000;
    }

    public String getBattleId() {
        return battleId;
    }

    public void setBattleId(String battleId) {
        this.battleId = battleId;
    }

    public String getPlayerIndex() {
        return playerIndex;
    }

    public void setPlayerIndex(String playerIndex) {
        this.playerIndex = playerIndex;
    }

    public String getServerHost() {
        return serverHost;
    }

    public void setServerHost(String serverHost) {
        this.serverHost = serverHost;
    }

    public int getServerPort() {
        return serverPort;
    }

    public void setServerPort(int serverPort) {
        this.serverPort = serverPort;
    }

    public SGCommonProto.E_BATTLE_TYPE getBattleType() {
        return battleType;
    }

    public void setBattleType(SGCommonProto.E_BATTLE_TYPE battleType) {
        this.battleType = battleType;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }


    @Override
    public String toString() {
        return "{" +
                "battleId='" + battleId + '\'' +
                ", playerIndex='" + playerIndex + '\'' +
                ", serverHost='" + serverHost + '\'' +
                ", serverPort=" + serverPort +
                ", createTime=" + createTime +
                ", battleType=" + battleType +
                '}';
    }
}
